//
//  DrawingBoardView.m
//  CoreAnimation
//
//  Created by 张国文 on 16/11/1.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "DrawingBoardView.h"

@interface DrawingBoardView ()

@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (nonatomic, strong) UIColor *color;

@end

@implementation DrawingBoardView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.bezierPath = [UIBezierPath bezierPath];
        [self.bezierPath setLineJoinStyle:kCGLineJoinRound];
        [self.bezierPath setLineCapStyle:kCGLineCapRound];
        [self.bezierPath setLineWidth:3.0f];
        [self changeColor];
    }
    return self;
}

- (void)changeColor
{
    UIButton *greenColor = [UIButton buttonWithType:UIButtonTypeCustom];
    greenColor.frame = CGRectMake(0, 0, 100, 30);
    greenColor.backgroundColor = [UIColor cyanColor];
    [greenColor setTitle:@"绿色" forState:UIControlStateNormal];
    [greenColor setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [greenColor addTarget:self action:@selector(greenColor:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:greenColor];
    
    UIButton *magentaColor = [UIButton buttonWithType:UIButtonTypeCustom];
    magentaColor.frame = CGRectMake(120, 0, 100, 30);
    magentaColor.backgroundColor = [UIColor cyanColor];
    [magentaColor setTitle:@"粉色" forState:UIControlStateNormal];
    [magentaColor setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [magentaColor addTarget:self action:@selector(magentaColor:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:magentaColor];
    
    UIButton *redColor = [UIButton buttonWithType:UIButtonTypeCustom];
    redColor.frame = CGRectMake(240, 0, 100, 30);
    redColor.backgroundColor = [UIColor cyanColor];
    [redColor setTitle:@"红色" forState:UIControlStateNormal];
    [redColor setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [redColor addTarget:self action:@selector(redColor:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:redColor];
}

- (void)greenColor:(UIButton *)button
{
    self.color = [UIColor greenColor];

}

- (void)magentaColor:(UIButton *)button
{
    self.color = [UIColor magentaColor];
}

- (void)redColor:(UIButton *)button
{
    self.color = [UIColor redColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    [self.bezierPath moveToPoint:point];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    [self.bezierPath addLineToPoint:point];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [self.color setStroke];
    [[UIColor clearColor] setFill];
    [self.bezierPath stroke];
}

@end
